﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using LPV_POS.mh_pos.mh_ban_hang;
//using LPV_POS.In;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang8_Tinh_Tien_BIZA : DevExpress.XtraEditors.XtraForm
    {
        DataTable tbHT_NGAY_LE = null;
        #region "   Khai bao"
        DataTable tbBH_PHIEU_TMP = null;
        DataTable tbBH_PHIEU_CT_TMP = null;
        DataTable tbBangGia = null;
        decimal dTong_Gio = 0;
        #endregion

        #region "   mh_banhang8_Tinh_Tien_BIZA"
        public mh_banhang8_Tinh_Tien_BIZA(DataTable _tbBH_PHIEU_TMP, DataTable _tbBH_PHIEU_CT_TMP)
        {
            InitializeComponent();
            tbBH_PHIEU_TMP = _tbBH_PHIEU_TMP;
            tbBH_PHIEU_CT_TMP = _tbBH_PHIEU_CT_TMP;
            tbBangGia = new thuvien().SP_SelectDataTable("sp_DM_TIENGIO1",
                                                        new string[] { "@flag", "@DM_BAN_MA"},
                                                        new object[] { 6, tbBH_PHIEU_TMP.Rows[0]["DM_BAN_MA"]});
            cboDonGia.DataSource = tbBangGia;
            grdBangGia.DataSource = tbBangGia; 

            this.SetgrdDonGia_DataSource();
            txtGhiChu.Text = _tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] + "";
            Boolean bKhongDuoc_Sua_GioRaVao = new cpublic().Registry_Get("chkKhongDuoc_Sua_GioRaVao") == "True";
            dtpVao.Enabled = !bKhongDuoc_Sua_GioRaVao;
            dtpRa.Enabled = !bKhongDuoc_Sua_GioRaVao;
            dtpRa.Value = Variable.ngay_gio_hienhanh;
        }
        #endregion

        #region "   SetgrdDonGia_DataSource"
        void SetgrdDonGia_DataSource()
        {
            try
            {
                DataTable tbDG = tbBH_PHIEU_CT_TMP.Copy();
                for (int i = 0; i < tbDG.Rows.Count; i++)
                {
                    if (tbDG.Rows[i]["MAHH"] + "" != "Tien_Gio")
                        tbDG.Rows.RemoveAt(i--);
                }
                for (int i = 0; i < tbDG.Rows.Count; i++)
                    tbDG.Rows[i]["STT"] = i + 1;
                grdTienGio_ChiTiet.DataSource = tbDG;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region "   mh_banhang8_Tinh_Tien_HOTELGio_Load"
        private void mh_banhang8_Tinh_Tien_HOTELGio_Load(object sender, EventArgs e)
        {
            this.ShowInfo();
        }
        #endregion

        #region "   btnThoat_Click"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
        #endregion

        #region "   btnThucHien_Click"
        private void btnThucHien_Click(object sender, EventArgs e)
        {//Luu Update
            try
            {
                //BH_PHIEU_TMP
                tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] = txtGhiChu.Text.Trim();
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_RA"] = dtpRa.Value;
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = dtpVao.Value;

                //BH_PHIEU_CT_TMP
                DataTable tbTienGio_ChiTiet = (DataTable)grdTienGio_ChiTiet.DataSource;

                string sExpression = tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].Expression;
                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].Expression = "";
                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].ReadOnly = false;
                tbTienGio_ChiTiet.Columns["THANH_TIEN"].Expression = "";
                tbTienGio_ChiTiet.Columns["THANH_TIEN"].ReadOnly = false;
                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].Expression = "";
                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].ReadOnly = false;
                tbTienGio_ChiTiet.Columns["GIA_BAN"].Expression = "";
                tbTienGio_ChiTiet.Columns["GIA_BAN"].ReadOnly = false;

                #region "   UPDATE lại dữ liệu TIỀN phòng vào tbBH_PHIEU_CT_TMP"
                float iSL = 0;
                int iDG = 0;
                double iGiamGia = 0;
                if (tbTienGio_ChiTiet != null)
                {
                    for (int i = 0; i < tbTienGio_ChiTiet.Rows.Count; i++)
                    {
                        if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length < i + 1)
                        {
                            DataRow dr = null;
                            dr = tbBH_PHIEU_CT_TMP.NewRow();
                            dr["MAHH"] = "Tien_Gio";
                            dr["CHIETKHAU_TIEN"] = 0;
                            dr["KM_TIEN"] = 0;
                            dr["VAT_TIEN"] = 0;
                            dr["GHI_CHU"] = "";
                            tbBH_PHIEU_CT_TMP.Rows.Add(dr);
                        }
                        //
                        float.TryParse(tbTienGio_ChiTiet.Rows[i]["SO_LUONG"] + "", out iSL);
                        int.TryParse(tbTienGio_ChiTiet.Rows[i]["DON_GIA"] + "", out iDG);

                        decimal dPhanTram = nudGiam_PhanTram.Value;

                        object iTT = iSL * iDG;
                        iGiamGia = this.tinh_phantram(iTT + "", dPhanTram.ToString().Replace(",", ""));
                        //
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["MAHH"] = tbTienGio_ChiTiet.Rows[i]["MAHH"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJNAME_HH_ROOT"] = tbTienGio_ChiTiet.Rows[i]["OBJNAME_HH_ROOT"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJNAME_HH"] = tbTienGio_ChiTiet.Rows[i]["OBJNAME_HH"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["SO_LUONG"] = tbTienGio_ChiTiet.Rows[i]["SO_LUONG"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["DON_GIA"] = tbTienGio_ChiTiet.Rows[i]["DON_GIA"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["CHIETKHAU_PHANTRAM"] = nudGiam_PhanTram.Value;
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["CHIETKHAU_TIEN"] = iGiamGia;
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["NGAY_TINH_GIO"] = dtpRa.Value;
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["GHI_CHU"] = tbTienGio_ChiTiet.Rows[i]["GHI_CHU"];
                    }
                }
                //int igio = 0;
                //for (int i = 0; i < tbBH_PHIEU_CT_TMP.Rows.Count; i++)
                //{
                //    if (tbBH_PHIEU_CT_TMP.Rows[i]["MAHH"] + "" == "Tien_Gio")
                //    {
                //        if (tbTienGio_ChiTiet.Select().Length > 0 && igio == 0)
                //        {//tam thoi TIEN GIO chi luu 1 dong
                //            DataRow drTMP = tbBH_PHIEU_CT_TMP.Rows[i];
                //            drTMP.ItemArray = tbTienGio_ChiTiet.Select()[0].ItemArray;
                //            drTMP["SO_LUONG"] = tbTienGio_ChiTiet.Select()[0]["SO_LUONG"];
                //        }
                //        else
                //        {//xoa nhung dong TIEN_GIO co STT > 1
                //            //xoa du lieu
                //            Int64 iobjid = 0;
                //            Int64.TryParse(tbBH_PHIEU_CT_TMP.Rows[i]["OBJID"] + "", out iobjid);
                //            if (iobjid > 0)
                //            {
                //                string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                //                new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                //            }
                //            tbBH_PHIEU_CT_TMP.Rows.RemoveAt(i--);
                //        }
                //        igio++;
                //    }
                //}

                //if (igio == 0)
                //{
                //    for (int i = igio + 1; i <= tbTienGio_ChiTiet.Rows.Count; i++)
                //    {
                //        DataRow drTMP = tbBH_PHIEU_CT_TMP.NewRow();
                //        drTMP.ItemArray = tbTienGio_ChiTiet.Select()[0].ItemArray;
                //        drTMP["SO_LUONG"] = tbTienGio_ChiTiet.Select()[0]["SO_LUONG"];
                //        tbBH_PHIEU_CT_TMP.Rows.Add(drTMP);
                //    }
                //}
                #endregion

                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].Expression = sExpression;
                tbTienGio_ChiTiet.Columns["THANH_TIEN"].Expression = sExpression;
                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].Expression = "DON_GIA";
                tbTienGio_ChiTiet.Columns["GIA_BAN"].Expression = "DON_GIA";

                #region "   Xoa nhung dong ko nam trong TIEN PHONG CHI TIET"
                for (int i = tbTienGio_ChiTiet.Rows.Count; i < tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length; i++)
                {
                    Int64 iobjid = 0;
                    Int64.TryParse(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJID"] + "", out iobjid);
                    if (iobjid > 0)
                    {
                        string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                        int irt = new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                    }
                    tbBH_PHIEU_CT_TMP.Rows.Remove(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]);
                    i--;

                }
                ////foreach (DataRow dr in tbBH_PHIEU_CT_TMP.Select("SO_LUONG=0"))
                //for (int i = 0; i < tbBH_PHIEU_CT_TMP.Rows.Count; i++)
                //{
                //    if (tbBH_PHIEU_CT_TMP.Rows[i]["MAHH"] + "" == "Tien_Gio")
                //    {
                //        if (tbBH_PHIEU_CT_TMP.Rows[i]["SO_LUONG"] + "" == "" || tbBH_PHIEU_CT_TMP.Rows[i]["SO_LUONG"] + "" == "0")
                //        {
                //            Int64 iobjid = 0;
                //            Int64.TryParse(tbBH_PHIEU_CT_TMP.Rows[i]["OBJID"] + "", out iobjid);
                //            if (iobjid > 0)
                //            {
                //                string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                //                new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                //            }
                //            tbBH_PHIEU_CT_TMP.Rows.RemoveAt(i--);
                //        }
                //    }
                //}                
                #endregion
                //BH_PHIEU_TMP
                tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] = txtGhiChu.Text.Trim();
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_RA"] = dtpRa.Value;
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = dtpVao.Value;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                MessageBox_LPV.Show("Thông báo\n" + ex.Message);
            }
        }
        #endregion

        #region "   tinh_phantram"
        Double tinh_phantram(string _sThanhTien, string phantram)
        {//tinh cho ca CHIET_KHAU va VAT

            double _ThanhTien = double.Parse(_sThanhTien.Replace(",",""));
            Double db = 0;
            try
            {
                db = Double.Parse("0" + phantram.Replace("%", ""));
                db = Math.Round(_ThanhTien * db / 100f / 100, 0) * 100;//lam tron 2 so (123,123.45 --> 123,100)
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return db;
        }
        #endregion

        #region "   Get_Tien_by"
        int Get_Tien_by(decimal _dTong_Gio)
        {
            
            int iTien = 0;
            try
            {
                int igio1 = 0;
                int iphut1 = 0;
                int igiay1 = 0;
                //
                int igio2 = 0;
                int iphut2 = 0;
                int igiay2 = 0;

                foreach (DataRow dr in tbBangGia.Rows)
                {
                    int.TryParse(dr["tu_gio_h"] + "", out igio1);
                    int.TryParse(dr["tu_gio_m"] + "", out iphut1);
                    int.TryParse(dr["tu_gio_s"] + "", out igiay1);
                    int.TryParse(dr["DEN_GIO_h"] + "", out igio2);
                    int.TryParse(dr["DEN_GIO_m"] + "", out iphut2);
                    int.TryParse(dr["DEN_GIO_s"] + "", out igiay2);

                    int iTu_Gio = igio1 * 3600 + iphut1 * 60 + igiay1;
                    int iDen_Gio = igio2 * 3600 + iphut2 * 60 + igiay2;
                    int iGio_Ra = dtpRa.Value.Hour * 3600 + dtpRa.Value.Minute * 60 + dtpRa.Value.Second;

                    if (igio1 * 3600 + iphut1 * 60 + igiay1 <= dtpRa.Value.Hour * 3600 + dtpRa.Value.Minute * 60-1// + dtpRa.Value.Second - 1
                        &&
                        igio2 * 3600 + iphut2 * 60 + igiay2 >= dtpRa.Value.Hour * 3600 + dtpRa.Value.Minute * 60-1// + dtpRa.Value.Second - 1
                        )
                    {
                        int.TryParse(dr[cboDonGia.ValueMember] + "", out iTien);
                        break;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Thông báo\n" + ex.Message, "Get_Tien_by(...)");
            }
            return iTien;
        }
        #endregion

        #region "   ShowForm"
        public DataTable ShowForm()
        {
            this.ShowDialog();
            return tbBH_PHIEU_CT_TMP;
        }
        #endregion

        #region "   ShowInfo"
        void ShowInfo()
        {
            DateTime dNGAY_GIO_VAO;
            if (tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "" == "")
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = Variable.ngay_gio_hienhanh;

            DateTime.TryParse(tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "", out dNGAY_GIO_VAO);
            dtpVao.Value = dNGAY_GIO_VAO;
            //
            //dtpRa.Value = Variable.ngay_gio_hienhanh;
            //
            decimal dGiam_PhanTram = 0;
            if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length > 0)
                decimal.TryParse(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[0]["CHIETKHAU_PHANTRAM"] + "", out dGiam_PhanTram);
            else
            {
                #region "   Giam Gia Default"
                cpublic ocpublic = new cpublic();
                string d1 = ocpublic.Registry_Get("dtpGiamTienGio_Tu");
                string d2 = ocpublic.Registry_Get("dtpGiamTienGio_Den");                
                if (d1.Length == 8 && d2.Length == 8)
                {
                    string d3 = ocpublic.Registry_Get("nudGiam_TienGio_PhanTram");
                    //tu ngay
                    string l1 = d1.Substring(0, 4);
                    string l2 = d1.Substring(4, 2);
                    string l3 = d1.Substring(6, 2);
                    DateTime dngay1 = new DateTime(int.Parse(l1), int.Parse(l2), int.Parse(l3), 0, 0, 0);

                    //den ngay
                    l1 = d2.Substring(0, 4);
                    l2 = d2.Substring(4, 2);
                    l3 = d2.Substring(6, 2);
                    DateTime dngay2 = new DateTime(int.Parse(l1), int.Parse(l2), int.Parse(l3), 23, 59, 59);

                    if (dngay1 <= dtpVao.Value && dtpVao.Value <= dngay2)
                    {
                        decimal dc = 0;
                        decimal.TryParse(d3, out dc);
                        dGiam_PhanTram = dc;
                    }
                }
                #endregion
            }
            nudGiam_PhanTram.Value = dGiam_PhanTram;
        }
        #endregion

        #region "   dtpVao_ValueChanged"
        private void dtpVao_ValueChanged(object sender, EventArgs e)
        {
          
            try
            {
                string sT = "";
                if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Monday)
                    sT = "T2";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Tuesday)
                    sT = "T3";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Wednesday)
                    sT = "T4";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Thursday)
                    sT = "T5";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Friday)
                    sT = "T6";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Saturday)
                    sT = "T7";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Sunday)
                    sT = "T1";

                lblNgay_Gio_Vao.Text = lblNgay_Gio_Vao.Tag + " (" + sT.Replace("T1", "CN") + ")";
                if (tbBangGia.Rows.Count > 0)
                {
                    cboDonGia.DisplayMember = sT + "_ten";
                    cboDonGia.ValueMember = sT;

                    decimal dgia = 0;
                    decimal.TryParse(tbBangGia.Rows[0][sT] + "", out dgia);
                    if (tbBangGia.Rows[0]["TINH_GIA_USD"] + "" == "True")
                    {
                        int iTI_GIA_USD = 0;
                        int.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["TI_GIA_USD"] + "", out iTI_GIA_USD);
                        dgia = dgia * iTI_GIA_USD;
                    }
                    //nudDonGia.Value = dgia;
                }

                if (dtpVao.Value >= dtpRa.Value)
                    dTong_Gio = 0;

                DataTable tbThanhTien = new thuvien().SP_SelectDataTable("sp_TinhTienGio",
                                                            new string[] { "@flag", "@ban_ma", "@gio_vao", "@gio_ra" },
                                                            new object[] { 2, tbBH_PHIEU_TMP.Rows[0]["DM_BAN_MA"], dtpVao.Value, dtpRa.Value });
                decimal dgio = 0;
                if (dtpVao.Value < dtpRa.Value)
                {
                    TimeSpan ts = dtpRa.Value.Subtract(dtpVao.Value);
                    dgio = (decimal)ts.Days * (decimal)24 + (decimal)ts.Hours + (decimal)ts.Minutes / (decimal)60;
                }
                dTong_Gio = Math.Round(dgio, 1);
                if (chkKaraokeAuTo.Checked) //if (grvTienGio_ChiTiet.RowCount == 0)
                    this.btnTinhTienGio_Click(null, null);
            }
            catch (Exception ex)
            {
                MessageBox_LPV.Show(""+ex.Message);
            }
        }
        #endregion

        #region "   nudTong_Gio_ValueChanged"
        private void nudTong_Gio_ValueChanged(object sender, EventArgs e)
        {
            if (cboDonGia.SelectedItem + "" != "")
            {
                decimal fDonGia = 0;
                decimal.TryParse(cboDonGia.Text, out fDonGia);
                DataRow dr = ((DataRowView)cboDonGia.SelectedItem).Row;
                int iGIA_USD = 1;
                //if (dr["TINH_GIA_USD"] + "" == "True")
                int.TryParse(dr["TINH_GIA_USD"] + "", out iGIA_USD);

                //nudThanhTien.Value = fDonGia * dTong_Gio * iGIA_USD;
            }
        }
        #endregion

        #region "   btnTinhTienGio_Click"
        private void btnTinhTienGio_Click(object sender, EventArgs e)
        {

            if (tbHT_NGAY_LE == null)
            {
                tbHT_NGAY_LE = new thuvien().SP_SelectDataTable("sp_HT_NGAY_LE",
                                               new string[] { "@flag", "@NAM" },
                                               new object[] { 5, dtpVao.Value.Year });
            }
            /////////////////////////////////////////

            //BH_PHIEU_CT_TMP
            DataTable tbBH_PHIEU_CT_TMP2011 = (DataTable)grdTienGio_ChiTiet.DataSource;
            int ra = int.Parse(dtpRa.Value.ToString("yyyyMMdd"));
            int vao = int.Parse(dtpVao.Value.ToString("yyyyMMdd"));

            int iSTT = 1;
            //int iRow = 0;

            //tu gio
            int igio1 = 0;
            int iphut1 = 0;
            int igiay1 = 0;
            //den gio
            int igio2 = 0;
            int iphut2 = 0;
            int igiay2 = 0;
            int iGia = 0;
            //DM
            float iGio_Vao = dtpVao.Value.Hour * 3600 + dtpVao.Value.Minute * 60;//--->Lấy ra giây
            TimeSpan tsp = dtpRa.Value.Subtract(dtpVao.Value);

            float iGio_Ra = dtpRa.Value.Hour * 3600 + dtpRa.Value.Minute * 60;//--->Lấy ra giây
            if (int.Parse(dtpRa.Value.ToString("yyyyMMdd")) > int.Parse(dtpVao.Value.ToString("yyyyMMdd")))
                iGio_Ra += 24 * 3600;
            float ivao = iGio_Vao = iGio_Vao / 3600f;//-->Tổng số giây giờ vào chia cho 3600 sẽ lấy ra được bao nhiêu giờ
            // ví dụ như 7200/3600=2 là vào 2h
          
            float ira = iGio_Ra = iGio_Ra / 3600f;
            
            Boolean ingayle = false;
            if (tbHT_NGAY_LE != null)
                ingayle = tbHT_NGAY_LE.Select("NGAY1 = " + dtpVao.Value.ToString("yyyyMMdd") + "").Length > 0;

            for (int i = 0; i <= ra - vao; i++)
            {
                if (i > 0)

                //if (iGio_Ra > 24)
                //    //---iGio_Ra=44>24
                {
                    iGio_Vao = -1;
                    iGio_Ra = iGio_Ra - 24;

                }
                foreach (DataRow drg in tbBangGia.Rows)//--->Lấy từng cột,dòng của bản tbBangGia trong stored
                {
                    if (ingayle)
                        int.TryParse(drg["T1"] + "", out iGia);
                    else
                        int.TryParse(drg[cboDonGia.ValueMember] + "", out iGia);
                    //
                    //tu gio

                    int.TryParse(drg["tu_gio_h"] + "", out igio1);
                    int.TryParse(drg["tu_gio_m"] + "", out iphut1);
                    int.TryParse(drg["tu_gio_s"] + "", out igiay1);
                    //den gio
                    int.TryParse(drg["DEN_GIO_h"] + "", out igio2);
                    int.TryParse(drg["DEN_GIO_m"] + "", out iphut2);
                    int.TryParse(drg["DEN_GIO_s"] + "", out igiay2);
                    //
                    float iDM_Vao = igio1 * 3600 + iphut1 * 60 + igiay1;//-->Số giây giờ vào có trong CSDL
                    //iGio_Vao là số giây trên form
                    float iDM_Ra = igio2 * 3600 + iphut2 * 60 + igiay2;
                    //
                    iDM_Vao = iDM_Vao / 3600f;//-->Lấy ra giờ vào ví dụ 1,2 giờ
                    iDM_Ra = iDM_Ra / 3600f;
                    string x = "";
                    //////////////////////////////////////////////
                    //---------------------------Điều kiện 1-----------------------------------
                    if (iGio_Vao >= iDM_Vao && iGio_Vao <= iDM_Ra)
                    {
                        if (iGio_Ra <= iDM_Ra)//-->Tính giờ trong khoảng đã có trong CSDL
                        {
                            float f = (iGio_Ra - iGio_Vao);
                            if (f > 0.005)
                                this.Insert_TienGio(iSTT++, tbBH_PHIEU_CT_TMP2011, iGio_Vao, iGio_Ra, iGia);
                            goto end;
                        }
                        else if (iGio_Ra > iDM_Ra)
                        {
                            this.Insert_TienGio(iSTT++, tbBH_PHIEU_CT_TMP2011, iGio_Vao, iDM_Ra, iGia);

                        }
                    }
                    //-----------------------Điều kiện 2------------------------
                    else if (iGio_Vao < iDM_Vao && iGio_Ra > iDM_Vao)
                    {
                        if (iGio_Ra <= iDM_Ra)
                            this.Insert_TienGio(iSTT++, tbBH_PHIEU_CT_TMP2011, iDM_Vao, iGio_Ra, iGia);
                        else if (iGio_Ra > iDM_Ra)
                            this.Insert_TienGio(iSTT++, tbBH_PHIEU_CT_TMP2011, iDM_Vao, iDM_Ra, iGia);
                    }

                }
            }
        end:

            if (iSTT < tbBH_PHIEU_CT_TMP2011.Rows.Count)
            {

                for (int i = (int)iSTT + 1; i <= tbBH_PHIEU_CT_TMP2011.Rows.Count; i++)
                {
                    tbBH_PHIEU_CT_TMP2011.Rows.RemoveAt(--i);
                }
            }

            #region "   Mearge "
            DataRow drTruoc = null;
            foreach (DataRow drtmp in tbBH_PHIEU_CT_TMP2011.Select("", "DON_GIA"))
            {
                if (drTruoc != null)
                {
                    if (drtmp["DON_GIA"] + "" == drTruoc["DON_GIA"] + "")
                    {
                        string s1 = drTruoc["Stt"] + "";
                        string s2 = drtmp["Stt"] + "";
                        float iSL1 = 0;
                        float.TryParse(drTruoc["SO_LUONG"] + "", out iSL1);
                        float iSL2 = 0;
                        float.TryParse(drtmp["SO_LUONG"] + "", out iSL2);
                        drTruoc["SO_LUONG"] = iSL1 + iSL2;
                        //drTruoc.ItemArray = drtmp.ItemArray;
                        tbBH_PHIEU_CT_TMP2011.Rows.Remove(drtmp);
                        tbBH_PHIEU_CT_TMP2011.Select("STT = " + s1)[0]["so_luong"] = iSL1 + iSL2;
                    }
                    else
                        drTruoc.ItemArray = drtmp.ItemArray;
                }
                else
                {
                    drTruoc = tbBH_PHIEU_CT_TMP2011.NewRow();
                    drTruoc.ItemArray = drtmp.ItemArray;
                }

   

            }
            #endregion
            grdTienGio_ChiTiet.DataSource = tbBH_PHIEU_CT_TMP2011;

        }

        void Insert_TienGio(int iSTT, DataTable _tbBH_PHIEU_CT_TMP2011, float gio1, float gio2, float igia)
        {
            #region "   DataRow dr = null;"

            DataRow dr = null;
            int iRow = 0;
            for (int i = 0; i < _tbBH_PHIEU_CT_TMP2011.Rows.Count; i++)//--Vòng lặp đầu tiên sẽ không thực hiện vì
            //-- bảng _tbBH_PHIEU_CT_TMP2011 chưa có dữ liệu
            {
                if (_tbBH_PHIEU_CT_TMP2011.Rows[i]["MAHH"] + "" == "Tien_Gio")
                {
                    if (++iRow == iSTT)
                    {
                        dr = _tbBH_PHIEU_CT_TMP2011.Rows[i];
                        break;
                    }
                }
            }
            #endregion

            if (dr == null)//Vòng lặp đầu tiên điều kiện này sẽ đúng
            {// neu ko co dong nao THOA DK
                dr = _tbBH_PHIEU_CT_TMP2011.NewRow();
                dr["MAHH"] = "Tien_Gio";
                _tbBH_PHIEU_CT_TMP2011.Rows.Add(dr);
                dr["CHIETKHAU_TIEN"] = 0;
                dr["KM_TIEN"] = 0;
                dr["VAT_TIEN"] = 0;
                dr["GHI_CHU"] = "";
            }//--Sau khi thực hiện các lệnh phù hợp với điều kiện trên thì bảng _tbBH_PHIEU_CT_TMP2011 sẽ có dữ liệu

            dr["STT"] = iSTT;
            dr["OBJNAME_HH_ROOT"] = "Tiền giờ";
            dr["OBJNAME_HH"] = "Tiền giờ";
            double iG = Math.Round(double.Parse((gio2 - gio1).ToString()), 2);//-----Làm tròn 0.00---20/07/2012 

            dr["SO_LUONG"] = iG;//Đầu tiên sẽ insert số lượng là 10=24-14h -->giờ vào

            dr["DON_GIA"] = igia;// this.Get_Tien_by(dTong_Gio);

            dr["NGAY_TINH_GIO"] = dtpRa.Value;


        }

        #endregion

        private void btntonggio_Click(object sender, EventArgs e)
        {
            try
            {
                mh_so omh_so = new mh_so("Tổng giờ", (float)dTong_Gio);
                decimal igio = omh_so.ShowFormD();
                if (omh_so.DialogResult == DialogResult.OK)
                    dTong_Gio = igio;
            }
            catch (Exception ex)
            {
                MessageBox_LPV.Show("Thông báo\n" + ex.Message);
            }
        }

        private void btnghichu_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string str = omh_chu.ShowForm("Ghi chú", txtGhiChu.Text, 300, 300, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtGhiChu.Text = str;
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            mh_banhang8_Giam_TienGio frm = new mh_banhang8_Giam_TienGio();
            frm.ShowDialog();
        }

        private void chkKaraokeAuTo_CheckedChanged(object sender, EventArgs e)
        {
            btnTinhTienGio.Enabled = !chkKaraokeAuTo.Checked;
            new cpublic().Registry_Set("chkKaraokeAuTo", chkKaraokeAuTo.Checked);
        }

    }
}